package com.tbit.uqbike.service.business.operationsanalysis.impl;

import com.tbit.manager.api.dto.MachineOperationsStatDTO;
import com.tbit.uqbike.object.pojo.UnifyManagementStat;
import com.tbit.manager.api.dto.MachineStatReturnDTO;
import com.tbit.uqbike.service.business.operationsanalysis.MachineOAService;
import com.tbit.uqbike.webmanager.business.vehicle_operation_report.dao.stat.LowBatteryHourDao;
import com.tbit.uqbike.webmanager.business.vehicle_operation_report.object.LowBatteryStat;
import com.tbit.uqbike.webmanager.dao.stat.MachineOperationsStatDao;
import com.tbit.uqbike.webmanager.dao.stat.UnifyManagementStatDao;
import com.tbit.utils.DateTimeUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 车辆分析Service接口实现
 *
 * @author <a href="mailto:chen.wutian@tbit.com.cn">BUGTian</a>
 * @since 2025/2/27
 */
@Service
public class MachineOAServiceImpl implements MachineOAService {

    @Autowired
    private MachineOperationsStatDao machineOperationsStatDao;
    @Autowired
    private UnifyManagementStatDao unifyManagementStatDao;
    @Autowired
    private LowBatteryHourDao lowBatteryHourDao;

    /**
     * @description: 获取产生订单的车辆折线表数据
     * @author: mo.shanyong
     * @date: 2025/3/4 9:25
     * @param: accountId 区域id
     * @param: startDate 开始时间
     * @param: endDate 结束时间
     * @return: java.util.List<com.tbit.manager.api.dto.MachineStatReturnDTO>
     */
    @Override
    public List<MachineStatReturnDTO> getOrderCarLineData(Integer accountId,String startTime,String endTime) {
        return machineOperationsStatDao.selectMachineOrderCountsInDay(accountId, startTime,endTime);
    }
    /**
     * @description: 按照短、中、长获取车辆闲置折线图数据
     * @author: mo.shanyong
     * @date: 2025/3/4 9:29
     * @param: accountId 区域id
     * @param: startDate 开始时间
     * @param: endDate 结束时间
     * @return: java.util.List < com.tbit.manager.api.dto.MachineOperationsStatDT>
     */
    @Override
    public List<MachineOperationsStatDTO> getIdleCarLineData(Integer accountId, String startDate, String endDate) {
        return machineOperationsStatDao.getIdleCarLineData(accountId, startDate, endDate);
    }

    /**
     * @description: 获取计费车辆分布折线图数据
     * @author: mo.shanyong
     * @date: 2025/3/4 10:01
     * @param: accountId 区域id
     * @param: startDate 日期：2025-03-01
     * @param: endDate 日期：2025-03-01
     * @return: java.util.List<com.tbit.manager.api.dto.MachineStatReturnDTO>
     */
    @Override
    public List<MachineStatReturnDTO> getChargingCarLineData(Integer accountId,String startDate,String endDate) {
        List<MachineStatReturnDTO> machineStatReturnList = new ArrayList<>();
        //24点是当天的数据
        List<UnifyManagementStat> unifyManagementStats = unifyManagementStatDao.queryAllByLimit(accountId, startDate, endDate, 24, null, null);
        if (CollectionUtils.isEmpty(unifyManagementStats)){
            return machineStatReturnList;
        }
        unifyManagementStats.forEach(unifyManagementStat -> {
            MachineStatReturnDTO machineStatReturn = new MachineStatReturnDTO();
            machineStatReturn.setTime(DateTimeUtil.getMDByDate(unifyManagementStat.getStatDate()));
            machineStatReturn.setValue(String.valueOf(Objects.nonNull(unifyManagementStat.getJfcls()) ? unifyManagementStat.getJfcls() : 0));
            machineStatReturnList.add(machineStatReturn);
        });
        return machineStatReturnList;
    }
    /**
     * @description: 低电占比折线图，如果只传开始时间，就返回当日的，两个都传，就返回传入区间的每天平均值
     * @author: mo.shanyong
     * @date: 2025/3/4 14:18
     * @param: accountId 区域id
     * @param: startDate 开始时间
     * @param: endDate 结束时间
     * @return: java.util.List<com.tbit.manager.api.dto.MachineStatReturnDTO>
     */
    @Override
    public List<MachineStatReturnDTO> getBatteryLineData(Integer accountId, Date startDate, Date endDate) {
        List<MachineStatReturnDTO> machineStatReturnList = new ArrayList<>();
        endDate = DateTimeUtil.getLastTimeOfDayByDate(endDate);
        List<LowBatteryStat> batteryAvgStatList = lowBatteryHourDao.queryDayAvgByDate(Collections.singletonList(accountId), startDate, endDate);
        if (CollectionUtils.isEmpty(batteryAvgStatList)){
            return machineStatReturnList;
        }
        batteryAvgStatList.forEach(lowBatteryStat -> {
            MachineStatReturnDTO machineStatReturn = new MachineStatReturnDTO();
            machineStatReturn.setTime(lowBatteryStat.getDataDate());
            machineStatReturn.setValue(lowBatteryStat.getCompleteEta());
            machineStatReturnList.add(machineStatReturn);
        });
        return machineStatReturnList;
    }
    /**
     * @description: 获取单日的车辆电量分布情况
     * @author: mo.shanyong
     * @date: 2025/3/4 15:29
     * @param: accountId 区域id
     * @param: startDate 开始时间
     * @return: java.util.List<com.tbit.manager.api.dto.MachineStatReturnDTO>
     */
    private List<MachineStatReturnDTO> getDayBatteryLineData(Integer accountId, Date startDate){
        Date endDate = DateTimeUtil.getLastTimeOfDayByDate(startDate);
        return lowBatteryHourDao.queryByDate(accountId, startDate, endDate);
    }
}
